<?php

class _Update
{
	var $textmode = false;
	var $uploadData;
	var $addons = array();
	var $files = array();
	var $locale;
	var $blinds = array();

	var $processTime;			// time() starting timestamp for enforceRules

	var $messages = array();
	var $errors = array();
	var $assignstr = '';
	var $assigngem = '';		// 2nd tracking property since we build a gem list while building an items list

	var $membersadded = 0;
	var $membersupdated = 0;
	var $membersremoved = 0;

	var $current_region = '';
	var $current_realm = '';
	var $current_guild = '';
	var $current_member = '';
	var $talent_build_urls = array();

	

	/**
	*
	*	file error upload handler
	*	returns true/false | sets error message with file name
	*/
	
	function upload_error_check($file)
	{
		global $roster;

		switch($file['error'])
		{
			case UPLOAD_ERR_OK:		  // Value: 0; There is no error, the file uploaded with success.
				return true;
			break;
 
			case UPLOAD_ERR_INI_SIZE:	// Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
				$this->setError('The uploaded file exceeds the upload_max_filesize directive in php.ini.','File Error ['.$file['name'].']');
				
			case UPLOAD_ERR_FORM_SIZE:   // Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
				$this->setError('The uploaded file exceeds the server maximum filesize allowed.','File Error ['.$file['name'].']');
				return false;
			break;
 
			case UPLOAD_ERR_PARTIAL:	 // Value: 3; The uploaded file was only partially uploaded.
				$this->setError('The uploaded file was only partially uploaded.','File Error ['.$file['name'].']');
				return false;
				break;
 
			case UPLOAD_ERR_NO_FILE:	 // Value: 4; No file was uploaded.
				$this->setError('No file was uploaded.','File Error ['.$file['name'].']');
				return false;
				break;
 
			case UPLOAD_ERR_NO_TMP_DIR:  // Value: 6; Missing a temporary folder.
				$output .= '<li>Missing a temporary folder. Please contact the admin.</li>';
				return false;
				break;
 
			case UPLOAD_ERR_CANT_WRITE:  // Value: 7; Failed to write file to disk.
				$output .= '<li>Failed to write file to disk. Please contact the admin.</li>';
				return false;
				break;
		}
	}

	/**
	 * Adds a message to the $messages array
	 *
	 * @param string $message
	 */
	function setMessage($message)
	{
		$this->messages[] = $message;
	}


	/**
	 * Returns all messages
	 *
	 * @return string
	 */
	function getMessages()
	{
		return implode("\n",$this->messages) . "\n";
	}


	/**
	 * Resets the stored messages
	 *
	 */
	function resetMessages()
	{
		$this->messages = array();
	}


	/**
	 * Adds an error to the $errors array
	 *
	 * @param string $message
	 */
	function setError( $message , $error )
	{
		$this->errors[] = array($message=>$error);
	}


	/**
	 * Gets the errors in wowdb
	 * Return is based on $mode
	 *
	 * @param string $mode
	 * @return mixed
	 */
	function getErrors( $mode='' )
	{
		if( $mode == 'a' )
		{
			return $this->errors;
		}

		$output = '';

		$errors = $this->errors;
		if( !empty($errors) )
		{
			$output = '<table width="100%" cellspacing="0">';
			$steps = 0;
			foreach( $errors as $errorArray )
			{
				foreach( $errorArray as $message => $error )
				{
					if( $steps == 1 )
					{
						$steps = 2;
					}
					else
					{
						$steps = 1;
					}

					$output .= "<tr><td class=\"membersRowRight$steps\">$error<br />\n"
							 . "$message</td></tr>\n";
				}
			}
			$output .= '</table>';
		}
		return $output;
	}

	/**
	 * DB insert code (former WoWDB)
	 */

	/**
	 * Resets the SQL insert/update string holder
	 */
	function reset_values()
	{
		$this->assignstr = '';
	}


	/**
	 * Add a value to an INSERT or UPDATE SQL string
	 *
	 * @param string $row_name
	 * @param string $row_data
	 */
	function add_value( $row_name , $row_data )
	{
		global $roster;

		if( $this->assignstr != '' )
		{
			$this->assignstr .= ',';
		}

		// str_replace added to get rid of non breaking spaces in cp.lua tooltips
		$row_data = str_replace(chr(194) . chr(160), ' ', $row_data);
		$row_data = stripslashes($row_data);
		$row_data = $this->escape($row_data);

		$this->assignstr .= " `$row_name` = '$row_data'";
	}
	function escape( $string )
	{
		if( version_compare( phpversion(), '4.3.0', '>' ) )
		{
			return mysql_real_escape_string( $string );
		}
		else
		{
			return mysql_escape_string( $string );
		}
	}


	/**
	 * Verifies existance of variable before attempting add_value
	 *
	 * @param array $array
	 * @param string $key
	 * @param string $field
	 * @param string $default
	 * @return boolean
	 */
	function add_ifvalue( $array , $key , $field=false , $default=false )
	{
		if( $field === false )
		{
			$field = $key;
		}

		if( isset($array[$key]) )
		{
			$this->add_value($field, $array[$key]);
			return true;
		}
		else
		{
			if( $default !== false )
			{
				$this->add_value($field, $default);
			}
			return false;
		}
	}

	/**
	 * Add a gem to an INSERT or UPDATE SQL string
	 * (clone of add_value method--this functions as a 2nd SQL insert placeholder)
	 *
	 * @param string $row_name
	 * @param string $row_data
	 */
	function add_gem( $row_name , $row_data )
	{
		global $roster;

		if( $this->assigngem != '' )
		{
			$this->assigngem .= ',';
		}

		$row_data = "'" . $this->escape($row_data) . "'";

		$this->assigngem .= " `$row_name` = $row_data";
	}


	/**
	 * Add a time value to an INSERT or UPDATE SQL string
	 *
	 * @param string $row_name
	 * @param array $date
	 */
	function add_time( $row_name , $date )
	{
		// 2000-01-01 23:00:00.000
		$row_data = $date['year'] . '-' . $date['mon'] . '-' . $date['mday'] . ' ' . $date['hours'] . ':' . $date['minutes'] . ':' . $date['seconds'];
		$this->add_value($row_name,$row_data);
	}


	/**
	 * Add a time value to an INSERT or UPDATE SQL string
	 *
	 * @param string $row_name
	 * @param string $date | UNIX TIMESTAMP
	 */
	function add_timestamp( $row_name , $date )
	{
		$date = date('Y-m-d H:i:s',$date);
		$this->add_value($row_name,$date);
	}

}
